$(".grid-item").click(function() {
	var obj = $(this);
	var id = $(this).attr("_id");
	var name = $(this).attr("cuisinename");


	if (obj.hasClass('f_in')) {
		obj.removeClass("f_in");
		obj.addClass("f_in_sel");
		addPurchaseItem(id, name, 1);
	} else {
		obj.removeClass("f_in_sel");
		obj.addClass("f_in");
		removeItem(id);
	}
});

$(".addIngredient").click(function() {
	var obj = $(this).find("i");
	if (obj.hasClass('fa-chevron-down')) {
		obj.removeClass("fa-chevron-down");
		obj.addClass("fa-chevron-up");
		$(".inputIngWin").show();

	} else {
		obj.removeClass("fa-chevron-up");
		obj.addClass("fa-chevron-down");
		$(".inputIngWin").hide();

	}
});



function filterIng() {
	var codition = "";
	var sx = $("#ingsx").val();
	if (sx != null && sx != "") {
		codition += "[sx*='" + sx + "']";
	}

	$(".grid-item").hide();
	$(".grid-item" + codition).show();
}

$('#ingsx').bind('input propertychange', function() {
	filterIng()
});
function addOtherItem() {
	var name = $("#ingaddname").val();
	var id = uuid();
	addPurchaseItem(id, name, 1);
}

function addPurchaseItem(id, name, type,dis,price,isInit) {
	isInit=isInit==null|isInit==""?false:isInit;
	if (name == null || name == "") {
		alert("名字不能为空");
		return false;
	}
	dis=dis==null||dis==""?1:dis;
	var addData={};
	addData["id"]=id;
	addData["name"]=name;
	addData["type"]=type;
	addData["dis"]=dis;
	addData["week"]=$("#week").val();
	
	if(isInit){
		var template = "<tr class='datatr' wsid='"
			+ id
			+ "' wsname='"
			+ name
			+ "' wstype='"
			+ type
			+ "'> "
			+ " <td>"
			+ name
			+ "</td>"
			+ " <td>"
			+ (type==1?"<a href='javascript:;' onclick='javascript:changeType(this)' >肉类</a>"
					:"<a href='javascript:;' onclick='javascript:changeType(this)' >菜类</a>")
			+ "</td>"
			+ " <td>"
			+ (dis==1?"<a href='javascript:;' onclick='javascript:changeStatus(this)' style='color:green'>销售中</a>"
					:"<a href='javascript:;' onclick='javascript:changeStatus(this)' style='color:red'>售罄</a>")
			+ "</td>"
			+ " <td>"
			+"<a href='javascript:;' onclick='javascript:changePrice(this)' style='color:green'>"+ (price==null||price=="undefined"?0:price)
			+ "</a></td>"

			+ " <td><i class='fa fa-minus-circle'  style='cursor:pointer;' onclick='removeItem(\""
			+ id + "\")'></i></td>" + " </tr>";
	var qobje = $(template);
	$("#cart").find(".addtr").before(qobje);
	}else{
	
		$.post("/weixinshow/add",addData,function(){
				var template = "<tr class='datatr' wsid='"
					+ id
					+ "' wsname='"
					+ name
					+ "' wstype='"
					+ type
					+ "'> "
					+ " <td>"
					+ name
					+ "</td>"
					+ " <td>"
					+ (type==1?"<a href='javascript:;' onclick='javascript:changeType(this)' >肉类</a>"
							:"<a href='javascript:;' onclick='javascript:changeType(this)' >菜类</a>")
					+ "</td>"
					+ " <td>"
					+ (dis==1?"<a href='javascript:;' onclick='javascript:changeStatus(this)' style='color:green'>销售中</a>"
							:"<a href='javascript:;' onclick='javascript:changeStatus(this)' style='color:red'>售罄</a>")
					+ "</td>"
					+ " <td><i class='fa fa-minus-circle'  style='cursor:pointer;' onclick='removeItem(\""
					+ id + "\")'></i></td>" + " </tr>";
			var qobje = $(template);
			$("#cart").find(".addtr").before(qobje);
	});	
	}
}


function removeItem(id) {
	$.get("/weixinshow/del",{"id":id,"week":$("#week").val()},function(){
		$(".grid-item[_id='" + id + "']").removeClass("f_in_sel");
		$(".grid-item[_id='" + id + "']").addClass("f_in");
		$("#cart").find("tr[wsid='" + id + "']").remove();
	});
}

function changePrice(obj){
	var id=$(obj).parent().parent().attr("wsid");
	var str=prompt("请输入修改的价钱",$(obj).html());
	var c=parseInt(str)
	var re = /^[0-9]*[1-9][0-9]*$/ ;
	if(re.test(str)){
		$.post("/weixinshow/update",{"price":c,"id":id,"week":$("#week").val()},function(){
			$(obj).html(c);
		});
	}else{
		$.post("/weixinshow/update",{"price":c,"id":id,"week":$("#week").val()},function(){
			$(obj).html(1);
		});
	}

}


function changeStatus(obj){
	var id=$(obj).parent().parent().attr("wsid");
	if($(obj).html()=="销售中"){
		$.post("/weixinshow/update",{"dis":"0","id":id,"week":$("#week").val()},function(){
			$(obj).css("color","red");
			$(obj).html("售罄");
		});
	}else{
		$.post("/weixinshow/update",{"dis":"1","id":id,"week":$("#week").val()},function(){
			$(obj).css("color","green");
			$(obj).html("销售中");
		});
	}
}

function changeType(obj){
	var id=$(obj).parent().parent().attr("wsid");
	if($(obj).html()=="肉类"){
		$.post("/weixinshow/update",{"cstype":"2","id":id,"week":$("#week").val()},function(){
			$(obj).html("菜类");
		});
	}else{
		$.post("/weixinshow/update",{"cstype":"1","id":id,"week":$("#week").val()},function(){
			$(obj).html("肉类");
		});
	}
}

